import { buildComment } from '../src/write';
import { FakedOctokit, fakedRepos } from './fakedOctokit';
import dedent from 'dedent';

interface RepositoryPayloadSubset {
    private: boolean;
    html_url: string;
}

const gitHubContext = {
    repo: {
        repo: 'repo',
        owner: 'user',
    },
    payload: {
        repository: {
            private: false,
            html_url: 'https://github.com/user/repo',
        } as RepositoryPayloadSubset | null,
    },
    workflow: 'Workflow name',
};

jest.mock('@actions/github', () => ({
    get context() {
        return gitHubContext;
    },
    getOctokit(token: string) {
        return new FakedOctokit(token);
    },
}));

afterEach(function () {
    fakedRepos.clear();
});

describe('buildComment', () => {
    it('should build comment when previous benchmarks are 0 and biggerIsBetter returns false', () => {
        const comment = buildComment(
            'TestSuite',
            {
                date: 12345,
                commit: {
                    id: 'testCommitIdCurrent',
                    author: {
                        name: 'TestUser',
                    },
                    committer: {
                        name: 'TestUser',
                    },
                    message: 'Test current commit message',
                    url: 'https://test.previous.commit.url',
                },
                tool: 'cargo',
                benches: [
                    {
                        value: 0,
                        name: 'TestBench<1>',
                        unit: 'testUnit',
                    },
                    {
                        value: 1,
                        name: 'TestBench<2>',
                        unit: 'testUnit',
                    },
                    {
                        value: -1,
                        name: 'TestBench<3>',
                        unit: 'testUnit',
                    },
                ],
            },
            {
                date: 12345,
                commit: {
                    id: 'testCommitIdPrevious',
                    author: {
                        name: 'TestUser',
                    },
                    committer: {
                        name: 'TestUser',
                    },
                    message: 'Test previous commit message',
                    url: 'https://test.previous.commit.url',
                },
                tool: 'cargo',
                benches: [
                    {
                        value: 0,
                        name: 'TestBench<1>',
                        unit: 'testUnit',
                    },
                    {
                        value: 0,
                        name: 'TestBench<2>',
                        unit: 'testUnit',
                    },
                    {
                        value: 0,
                        name: 'TestBench<3>',
                        unit: 'testUnit',
                    },
                ],
            },
        );

        expect(comment).toEqual(dedent`
            # TestSuite
            
            <details>
            
            | Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio |
            |-|-|-|-|
            | \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` |
            | \`TestBench<2>\` | \`1\` testUnit | \`0\` testUnit | \`+∞\` |
            | \`TestBench<3>\` | \`-1\` testUnit | \`0\` testUnit | \`-∞\` |
            
            </details>
            
            This comment was automatically generated by [workflow](https://github.com/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).
        `);
    });

    it('should build comment when current benchmarks are 0 and biggerIsBetter returns true', () => {
        const comment = buildComment(
            'TestSuite',
            {
                date: 12345,
                commit: {
                    id: 'testCommitIdCurrent',
                    author: {
                        name: 'TestUser',
                    },
                    committer: {
                        name: 'TestUser',
                    },
                    message: 'Test current commit message',
                    url: 'https://test.previous.commit.url',
                },
                tool: 'benchmarkjs',
                benches: [
                    {
                        value: 0,
                        name: 'TestBench<1>',
                        unit: 'testUnit',
                    },
                    {
                        value: 0,
                        name: 'TestBench<2>',
                        unit: 'testUnit',
                    },
                    {
                        value: 0,
                        name: 'TestBench<3>',
                        unit: 'testUnit',
                    },
                ],
            },
            {
                date: 12345,
                commit: {
                    id: 'testCommitIdPrevious',
                    author: {
                        name: 'TestUser',
                    },
                    committer: {
                        name: 'TestUser',
                    },
                    message: 'Test previous commit message',
                    url: 'https://test.previous.commit.url',
                },
                tool: 'benchmarkjs',
                benches: [
                    {
                        value: 0,
                        name: 'TestBench<1>',
                        unit: 'testUnit',
                    },
                    {
                        value: 1,
                        name: 'TestBench<2>',
                        unit: 'testUnit',
                    },
                    {
                        value: -1,
                        name: 'TestBench<3>',
                        unit: 'testUnit',
                    },
                ],
            },
        );

        expect(comment).toEqual(dedent`
            # TestSuite
            
            <details>
            
            | Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio |
            |-|-|-|-|
            | \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` |
            | \`TestBench<2>\` | \`0\` testUnit | \`1\` testUnit | \`+∞\` |
            | \`TestBench<3>\` | \`0\` testUnit | \`-1\` testUnit | \`-∞\` |
            
            </details>
            
            This comment was automatically generated by [workflow](https://github.com/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).
        `);
    });
});
